package mark.tom.fiter;

import java.net.Proxy;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.X509TrustManager;

import mark.tom.fiter.bean.config.OkHttpConfig;
import mark.tom.fiter.http.ssl.AbstractSSLSocketFactory;
import mark.tom.fiter.http.ssl.NoneSSLSocketFactory;
import okhttp3.CookieJar;
import okhttp3.Dns;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;

public class OkHttpClientProvider {

    private static final OkHttpClientProvider ourInstance = new OkHttpClientProvider();

    public static OkHttpClientProvider getInstance() {
        return ourInstance;
    }

    // OkHttpClient
    private OkHttpClient mClient = null;
    // OkHttp配置类
    private OkHttpConfig mConfig = null;

    private OkHttpClientProvider() {
    }

    public void initOkHttpConfig(OkHttpConfig mConfig) {
        this.mConfig = mConfig;
    }

    public OkHttpClient getOkHttpClient() {
        if (null == mClient) {
            mClient = getDefaultClient();
        }
        return mClient;
    }

    private OkHttpClient getDefaultClient() {
        if (null == mConfig) {
            mConfig = getDefaultConfig();
        }
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .followRedirects(true)
                .followSslRedirects(true)
                .retryOnConnectionFailure(true);
        builder.readTimeout(mConfig.getReadTimeOut(), TimeUnit.SECONDS);
        builder.writeTimeout(mConfig.getWriteTimeOut(), TimeUnit.SECONDS);
        builder.connectTimeout(mConfig.getConnectTimeOut(), TimeUnit.SECONDS);
        // 添加DNS配置
        Dns httpDns = mConfig.getDns();
        if (null != httpDns) {
            builder.dns(httpDns);
        }
        CookieJar cookieJar = mConfig.getCookieJar();
        if (null != cookieJar) {
            builder.cookieJar(cookieJar);
        }
        // 添加Response拦截器配置
        List<Interceptor> networkInterceptors = mConfig.getResponseInterceptors();
        if (null != networkInterceptors && networkInterceptors.size() > 0) {
            builder.networkInterceptors().addAll(networkInterceptors);
        }
        // 添加Request拦截器配置
        List<Interceptor> interceptors = mConfig.getRequestInterceptors();
        if (null != interceptors && interceptors.size() > 0) {
            builder.interceptors().addAll(interceptors);
        }
        // 设置是否允许网络代理
        if (!mConfig.isUseProxy()) {
            builder.proxy(Proxy.NO_PROXY);
        }
        // 设置认证证书
        AbstractSSLSocketFactory sslSocketFactory = mConfig.getSslSocketFactory();
        if (null != sslSocketFactory) {
            X509TrustManager trustManager = sslSocketFactory.getTrustManager();
            HostnameVerifier hostnameVerifier = sslSocketFactory.getHostnameVerifier();
            builder.sslSocketFactory(sslSocketFactory, trustManager)
                    .hostnameVerifier(hostnameVerifier);
        }
        return builder.build();
    }

    private OkHttpConfig getDefaultConfig() {
        return new OkHttpConfig().setSslSocketFactory(NoneSSLSocketFactory.getHttpSSLSocketFactory());
    }
}
